using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Threading.Tasks;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace BaseData.RazorLib.Services
{
    public class CustomerService : ICustomerService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public CustomerService(IHttpClientFactory httpClientFactory)
        {
            this._httpClientFactory = httpClientFactory;
        }

        public async Task<Result<CustomerInfoResponseDto>?> CreateCustomer(CustomerEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response =
                    await httpClient.PostAsJsonAsync($"/{ApiRoutes.Customer.CreateCustomer}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<CustomerInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<CustomerInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<CustomerInfoResponseDto>?> GetCustomer(int key)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Customer.GetCustomer.Replace("{id}", key.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<CustomerInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<IList<CustomerInfoResponseDto>>?> QueryCustomers(CustomerSearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.Customer.QueryCustomers));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<IList<CustomerInfoResponseDto>>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<PaginatedResult<CustomerInfoResponseDto>?> QueryCustomersByPage(CustomerSearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.Customer.QueryCustomers));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<CustomerInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<CustomerInfoResponseDto>?> UpdateCustomer(int id, CustomerEditRequestDto editRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.Customer.UpdateCustomer.Replace("{id}", id.ToString())}", editRequestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<CustomerInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<CustomerInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}